`
https://leetcode.cn/problems/kth-smallest-element-in-a-bst/
`

/**
 * Definition for a binary tree node.
 * function TreeNode(val, left, right) {
 *     this.val = (val===undefined ? 0 : val)
 *     this.left = (left===undefined ? null : left)
 *     this.right = (right===undefined ? null : right)
 * }
 */
/**
 * @param {TreeNode} root
 * @param {number} k
 * @return {number}
 */
var kthSmallest = function (root, k) {
  let res = -1
  let count = 0

  const traserve = (node) => {
    // 题目范围里 node.val 不会是负数
    // 所以可以拿来判断是不是已经找到结果了
    // 找到结果也即不需要继续遍历了
    if (node === null || res !== -1) {
      return
    }

    traserve(node.left)
    // 中序位置
    count++
    if (count === k) {
      res = node.val
      return
    }
    traserve(node.right)
  }

  traserve(root)
  return res
};